<?php
namespace app\controller;

use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;

class Down extends BaseController
{
    public function down($id){
        try {
            $u = $this->M('files')->field('path,name')->where(['id' => $id])->find();
            if(!empty($u)){
                return response(readfile($u['path']))->header(['Content-Length'=>filesize($u['path']),'Content-Disposition'=>'attachment; filename='.$u['name']])->contentType('application/octet-stream');
            }
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return pr(302);
        }
        return response();
    }
}
